package com.itheima.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Company;
import com.itheima.domain.store.Course;
import com.itheima.domain.store.Question;
import com.itheima.domain.system.Dept;
import com.itheima.utils.BeanUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

@WebServlet("/store/question/*")
public class QuestionServlet extends BaseServlet {
    //查找所有
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        //获取分页参数
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");

        //调用方法获取所有信息
        List<Question> list = questionServive.findAll(pageNum, pageSize);

        //使用分页插件处理
        PageInfo<Question> pageInfo = new PageInfo<>(list);

        //放入绘会话域
        req.setAttribute("page", pageInfo);

        //重定向到list.jsp页面
        req.getRequestDispatcher("/pages/store/question/list.jsp").forward(req, resp);

    }

    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        //调用方法获取所有信息
        List<Company> list = companyServive.list();
        //放入绘会话域
        req.setAttribute("companyList", list);

        //调用方法获取所有信息
        List<Catalog> list1 = catalogServive.list();

        //放入绘会话域
        req.setAttribute("catalogList", list1);

        //重定向到add.jsp页面
        req.getRequestDispatcher("/pages/store/question/add.jsp").forward(req, resp);

    }

    //添加
    public void save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //确认该操作是否支持文件上传操作 enctype="multipart/form-data"
        if(ServletFileUpload.isMultipartContent(req)){
            //创建磁盘工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //Servlet文件上传核心对象
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            //从request中读取数据
            List<FileItem> fileItems = fileUpload.parseRequest(req);
            // --处理form表单提交过来的普通数据
            //将数据获取到，封装成一个对象
            Question question = BeanUtil.fillBean(fileItems, Question.class);
            //调用新增方法
           String picture =  questionServive.save(question);

            // --处理form表单提交过来的文件数据
            for (FileItem item : fileItems) {
                //当前表单是否是文件表单
                if (!item.isFormField()) {
                    //从临时存储文件的地方将内容写到指定位置
                    item.write(new File(this.getServletContext().getRealPath("upload"), picture));

                }
            }
        }

        //调用查找所有方法
        findAll(req,resp);

    }

    //根据id删除
    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取删除参数
        String ids = req.getParameter("ids");

        //调用删除方法
        Boolean flag = questionServive.deleteByIds(ids);

        //如果删除成功调用查找所有，如果失败显示到错误页面
        if(flag){
            findAll(req,resp);
        }else {
            req.getRequestDispatcher("/pages/error.jsp").forward(req,resp);
        }
    }

    //根据id查找
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取查找参数
        String id = req.getParameter("id");

        //调用查找方法
        Question question = questionServive.findById(id);

        //将查找对象存储入会话域
        req.setAttribute("question",question);
        //调用方法获取所有信息
        List<Company> list = companyServive.list();
        //放入绘会话域
        req.setAttribute("companyList", list);

        //调用方法获取所有信息
        List<Catalog> list1 = catalogServive.list();

        //放入绘会话域
        req.setAttribute("catalogList", list1);

        //跳转到更新页面
        req.getRequestDispatcher("/pages/store/question/update.jsp").forward(req,resp);
    }

    //根据id更新
    public void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //1.确认该操作是否支持文件上传操作，enctype="multipart/form-data"
        if(ServletFileUpload.isMultipartContent(req)){
            //2.创建磁盘工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //3.Servlet文件上传核心对象
            ServletFileUpload fileUpload = new ServletFileUpload(factory);
            //4.从request中读取数据
            List<FileItem> fileItems = fileUpload.parseRequest(req);
            //创建一个标记位,标记当前时候有上传文件的操作
            boolean flag = false;
            for(FileItem item :fileItems){
                if(StringUtils.isNotBlank(item.getName())){
                    flag = true;
                    break;
                }
            }
            // --处理form表单提交过来的普通数据
            //将数据获取到，封装成一个对象
            Question question = BeanUtil.fillBean(fileItems,Question.class);
            //调用业务层接口save
            questionServive.update(question,flag);
            // --处理form表单提交过来的文件数据
            for(FileItem item : fileItems){
                //5.当前表单是否是文件表单
                if(!item.isFormField()){
                    //6.从临时存储文件的地方将内容写入到指定位置
                    item.write(new File(this.getServletContext().getRealPath("upload"),question.getId()));
                }
            }
        }
        findAll(req,resp);
    }
}
